﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Windows.Input;

namespace SmartPlant.Core.ViewModels
{
    public abstract class SmartDialogBase : BindableBase, IDialogAware
    {
        private DelegateCommand<string> _closeDialogCommand;

        private string _title = "询问";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public event Action<IDialogResult> RequestClose;

        public ICommand CloseDialogCommand
        {
            get
            {
                if (_closeDialogCommand != null) return _closeDialogCommand;
                _closeDialogCommand = new DelegateCommand<string>(ExecuteCloseDialogCommand);
                return _closeDialogCommand;
            }
        }

        public virtual void ExecuteCloseDialogCommand(string parameter)
        {
            ButtonResult result = ButtonResult.None;
            if (parameter?.ToLower() == "true")
                result = ButtonResult.Yes;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.No;
            RraiseRequestClose(new DialogResult(result));
        }

        //触发窗体关闭事件
        public virtual void RraiseRequestClose(DialogResult dialogresult)
        {
            RequestClose?.Invoke(dialogresult);
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            Message = parameters.GetValue<string>("message");
        }
    }
}
